package cn.stylefeng.guns.modular.rest.pack;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.packreview.model.params.PackReviewDetailsParam;
import cn.stylefeng.guns.modular.packreview.model.params.PackReviewParam;
import cn.stylefeng.guns.modular.packreview.model.result.PackReviewDetailsResult;
import cn.stylefeng.guns.modular.packreview.model.result.PackReviewResult;
import cn.stylefeng.guns.modular.packreview.service.PackReviewDetailsService;
import cn.stylefeng.guns.modular.packreview.service.PackReviewService;
import cn.stylefeng.guns.modular.replenishment.entity.ReplenishmentOrder;
import cn.stylefeng.guns.modular.replenishment.model.params.ReplenishmentOrderParam;
import cn.stylefeng.guns.modular.replenishment.model.result.ReplenishmentOrderResult;
import cn.stylefeng.guns.modular.replenishment.service.ReplenishmentOrderService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.rest.bh.UpdateReplenishIby;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 打包复核
 * 主要用于打包运单的列表展示
 * @author wp
 * @date 2025-07-11
 */
@Controller
@RequestMapping("/rest/pack")
public class restPackController extends BaseController {

    @Autowired
    private UserAop userAop;
    @Autowired
    private DeptService deptService;

    @Autowired
    private ReplenishmentOrderService replenishmentOrderService;
    @Autowired
    private PackReviewService packReviewService;
    @Autowired
    private PackReviewDetailsService packReviewDetailsService;
    /**
     * 补货列表
     * @param packReviewParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/packList")
    public LayuiPageInfo list(PackReviewParam packReviewParam, String condition) {
        User user = userAop.getUser();
        Long deptId = user.getDeptId();
        packReviewParam.setReviewStatus("未复核");
        packReviewParam.setDeptId(deptId);
        LayuiPageInfo pageBySpec = packReviewService.findPageBySpec(packReviewParam, condition);

        List<PackReviewResult> packReviewResults = pageBySpec.getData();
        for (PackReviewResult packReviewResult : packReviewResults) {
            PackReviewDetailsParam packReviewDetailsParam = new PackReviewDetailsParam();
            packReviewDetailsParam.setTrackNumber(packReviewResult.getTrackNumber());
            List<PackReviewDetailsResult> packReviewDetailResults = packReviewDetailsService.findListBySpec(packReviewDetailsParam);
            packReviewResult.setPackReviewDetailResults(packReviewDetailResults);
            packReviewResult.setTotalNum(packReviewDetailResults.size());
        }
        return pageBySpec;
    }

    /**
     * 修改打包单状态
     * @param packReviewParam
     * @param condition
     * @return
     */
    @ResponseBody
    @RequestMapping("/updatePackReview")
    public ResponseData updatePackReview(PackReviewParam  packReviewParam, String condition) {
        packReviewParam.setReviewStatus("已复核");
        packReviewParam.setReviewTime(ToolUtil.currentTime());
        //判断是否是秒杀商品
        if(packReviewParam.getOrderType().contains("秒杀")){
            packReviewParam.setTrackNumber(null);
        }
        packReviewService.updateByTrackNumber(packReviewParam);
        return ResponseData.success();
    }

    @ResponseBody
    @RequestMapping("/packDetailList")
    public ResponseData packDetailList(PackReviewDetailsParam  packReviewDetailsParam, String condition) {
        List<PackReviewDetailsResult> packReviewDetailResults = packReviewDetailsService.findListBySpec(packReviewDetailsParam);
        return ResponseData.success(packReviewDetailResults);
    }

    /**
     * 根据ID获取补货单信息
     * @param replenishmentOrderParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/getById")
    public ResponseData getById(ReplenishmentOrderParam replenishmentOrderParam) {
        ReplenishmentOrder replenishmentOrder = replenishmentOrderService.getById(replenishmentOrderParam.getId());
        return ResponseData.success(replenishmentOrder);
    }

    /**
     * 补货完成
     * @param updateReplenish
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateReplenish")
    public ResponseData updateReplenish(UpdateReplenishIby updateReplenish) {
        if(ToolUtil.isNotEmpty(updateReplenish.getId())){
            replenishmentOrderService.lambdaUpdate()
                    .set(ReplenishmentOrder::getStatus,"已完成")
                    .set(ReplenishmentOrder::getOperatTime,ToolUtil.currentTime())
                    .set(ReplenishmentOrder::getOperator,userAop.getUser().getName())
                    .eq(ReplenishmentOrder::getId,updateReplenish.getId())
                    .update();
        }
        return ResponseData.success();
    }

    /**
     * 复核打包单商品
     * @param packReviewDetailsParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/packReviewGoods")
    public ResponseData packReviewGoods(PackReviewDetailsParam  packReviewDetailsParam) {


        //判断是否是秒杀商品
        if(packReviewDetailsParam.getOrderType().contains("秒杀")){
            //运输单号
            String trackNumber = packReviewDetailsParam.getTrackNumber();
            //提货单号
            String pickNumber = packReviewDetailsParam.getPickNumber();

            //根据pickNumber 查询运输单
            PackReviewParam packReviewParam = new PackReviewParam();
            packReviewParam.setPickNumber(pickNumber);
            List<PackReviewResult> packReviewResults = packReviewService.findListBySpec(packReviewParam);
            for (PackReviewResult packReviewResult : packReviewResults) {

                PackReviewDetailsParam packReviewDetailsParam1 = new PackReviewDetailsParam();
                packReviewDetailsParam1.setTrackNumber(packReviewResult.getTrackNumber());
                packReviewDetailsParam1.setPickNumber(pickNumber);
                packReviewDetailsParam1.setReviewStatus("已复核");
                packReviewDetailsParam1.setReviewTime(ToolUtil.currentTime());
                packReviewDetailsParam1.setReviewer(userAop.getUser().getName());
                packReviewDetailsParam1.setGoodsNum(packReviewDetailsParam.getGoodsNum());
                packReviewDetailsParam1.setGoodsCode(packReviewDetailsParam.getGoodsCode());
                packReviewDetailsService.updatebyTrackNumberAndPickNumber(packReviewDetailsParam1);
            }
            return ResponseData.success();
        }



        System.out.println("packReviewDetailsParam"+packReviewDetailsParam);
        packReviewDetailsParam.setReviewStatus("已复核");
        packReviewDetailsParam.setReviewTime(ToolUtil.currentTime());
        packReviewDetailsParam.setReviewer(userAop.getUser().getName());
        packReviewDetailsService.update(packReviewDetailsParam);
        return ResponseData.success();
    }

    /**
     * 撤销复核打包单商品
     * @param packReviewDetailsParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/revokePackReviewGoods")
    public ResponseData revokePackReviewGoods(PackReviewDetailsParam  packReviewDetailsParam) {

        System.out.println("packReviewDetailsParam"+packReviewDetailsParam);
        packReviewDetailsParam.setReviewStatus("未复核");
        packReviewDetailsParam.setReviewTime(null);
        packReviewDetailsParam.setReviewer(null);
        packReviewDetailsService.revokePackReviewGoods(packReviewDetailsParam);
        return ResponseData.success();
    }


    public static void main(String[] args) {
        String str = "123456789012345秒杀678901234567";
        String str1 = "123456789012345678901234567";
        System.out.println(str.contains("秒杀"));
        System.out.println(str1.contains("秒杀"));
    }
}
